package tt.com.datamanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.ColdRoom;
import tt.com.common.model.YxCommonProviders;
import tt.com.common.util.PageUtil;

import tt.com.datamanager.mapper.ColdRoomMappers;
import tt.com.datamanager.service.ColdRoomService;

import java.util.Date;
import java.util.List;

@Service
public class ColdRoomServiceImpl extends ServiceImpl<ColdRoomMappers,ColdRoom> implements ColdRoomService {

    @Autowired
    private ColdRoomMappers coldRoomMappers;

    @Override
    public PageUtil selectAllColdRoomByPage(PageUtil pageUtil) {
        Page<ColdRoom> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<ColdRoom> coldRoomPage = baseMapper.selectPage(page, null);
        // 设置总条数 自动计算总页数
        pageUtil.setTotalSize((int) coldRoomPage.getTotal());
        pageUtil.setData(coldRoomPage);
        return pageUtil;
    }

    @Override
    public Integer updateColdRoom(ColdRoom coldroom, String updateUserId) {
        // 设置修改时间
        coldroom.setLastUpdateTime(new Date());
        // 设置修改操作人id
        coldroom.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(coldroom);


    }

    @Override
    public Integer deleteColdRoom(Long id, String deleteUserId) {
        //根据id获取冷藏柜
        ColdRoom coldRoom = baseMapper.selectById(id);
        if (coldRoom == null){
            //若数据不存在，返回-1
            return -1;
        }
        if (coldRoom.getDataStatus() == 1){
            //判断是否已经被删除过
            return -2;
        }
        //设置删除时间
        coldRoom.setDeleteTime(new Date());
        //设置删除操作人id
        coldRoom.setDeleteUserId(deleteUserId);
        coldRoom.setDataStatus(1);
        baseMapper.updateById(coldRoom);
        return baseMapper.deleteById(coldRoom.getId());
    }

    @Override
    public Integer insertColdRoom(ColdRoom coldRoom) {
        QueryWrapper<ColdRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("lattice_num", coldRoom.getLatticeNum());
        if (baseMapper.selectOne(wrapper) != null) {
            // 重复添加返回-1
            return -1;
        }
        // 设置创建时间
        coldRoom.setCreateTime(new Date());
        coldRoom.setDataStatus(0);
        return baseMapper.insert(coldRoom);
    }






    /*@Override
    public Integer batchInsert(List<ColdRoom> list){
        QueryWrapper<ColdRoom> wrapper = new QueryWrapper<>();
        //wrapper.eq("lattice_num",list.get(0).getLatticeNum());
        //wrapper.lambda().eq(ColdRoom::getLatticeNum,list.get(0).getLatticeNum());
        *//*for (int i = 0;i<=list.size()-1;i++){
            wrapper.eq("lattice_num",list.get(i).getLatticeNum());

        }*//*
        List<ColdRoom> coldRooms = coldRoomMappers.selectList(wrapper);
        //boolean flag = false;
        if (coldRoomMappers.selectList(wrapper) == null){
            for (ColdRoom coldRoom : list) {
                // 设置创建时间
                coldRoom.setCreateTime(new Date());
                // 设置创建操作人id
                coldRoom.setCreateUserId(list.get(0).getCreateUserId());
            }
            return coldRoomMappers.batchInsert(list);
        }else{
           for (int i = 0; i <= coldRooms.size()-1; i++) {
               for (int j = 0; j <= list.size()-1; j++) {
                   if (coldRooms.get(i).getLatticeNum().equals(list.get(j).getLatticeNum())){
                       return -1;
                   }
               }
           }
            for (ColdRoom coldRoom : list) {
                // 设置创建时间
                coldRoom.setCreateTime(new Date());
                // 设置创建操作人id
                coldRoom.setCreateUserId(list.get(0).getCreateUserId());
            }
            return coldRoomMappers.batchInsert(list);
       }

    }*/

}
